package top.skihub.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.skihub.mapper.GoodsMapper;
import top.skihub.mapper.ShoppingTrolleyMapper;
import top.skihub.pojo.Goods;
import top.skihub.pojo.ShoppingTrolley;
import top.skihub.service.ShoppingTrolleyService;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingTrolleyServiceImpl implements ShoppingTrolleyService {
    @Autowired
    private ShoppingTrolleyMapper shoppingTrolleyMapper;
    
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void addShoppingTrolley(Integer goodsId, Integer userId) {
        // 参数验证
        if (goodsId == null || userId == null) {
            throw new IllegalArgumentException("商品ID和用户ID不能为空");
        }

        try {
            // 1. 检查商品是否已在购物车中
            ShoppingTrolley existing = shoppingTrolleyMapper.selectByGoodsIdAndUserId(goodsId, userId);
            if (existing != null) {
                throw new IllegalStateException("该商品已在购物车中");
            }

            // 2. 获取商品信息
            Goods goods = goodsMapper.selectById(goodsId);
            if (goods == null) {
                throw new IllegalArgumentException("商品不存在");
            }

            // 3. 创建购物车记录
            ShoppingTrolley trolley = new ShoppingTrolley();
            trolley.setUserId(userId);
            trolley.setGoodsId(goodsId);
            trolley.setGoodsImg(goods.getGoodsImg());
            trolley.setGoodsName(goods.getGoodsName());
            trolley.setGoodsType(goods.getGoodsType());
            trolley.setGoodsDesc(goods.getGoodsDesc());
            trolley.setBusinessInfo(goods.getBusinessInfo());
            trolley.setPrice(goods.getPrice()); // 直接设置价格，因为都是Long类型
            trolley.setGoodsEnterTime2(LocalDateTime.now()); // 设置加入购物车时间

            // 4. 插入购物车记录
            int rows = shoppingTrolleyMapper.insert(trolley);
            if (rows != 1) {
                throw new RuntimeException("添加购物车失败");
            }
            
            log.info("成功添加商品到购物车: userId={}, goodsId={}, goodsName={}", 
                    userId, goodsId, goods.getGoodsName());
        } catch (Exception e) {
            log.error("添加购物车失败: userId={}, goodsId={}", userId, goodsId, e);
            throw e;
        }
    }

    @Override
    public List<ShoppingTrolley> getShoppingTrolley(Integer userId) {
        if (userId == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }
        return shoppingTrolleyMapper.getShoppingTrolley(userId);
    }

    @Override
    public void deleteShoppingTrolley(Integer goodsId, Integer userId) {
        if (goodsId == null || userId == null) {
            throw new IllegalArgumentException("商品ID和用户ID不能为空");
        }
        shoppingTrolleyMapper.deleteShoppingTrolley(goodsId, userId);
        log.info("成功从购物车删除商品: userId={}, goodsId={}", userId, goodsId);
    }
}
